package com.rfsp.mcp.controller.bank;
import com.rfsp.common.util.FileUtil;
import com.rfsp.mcp.pojo.po.AgreementRecordPO;
import com.rfsp.mcp.pojo.vo.PfmVO;
import com.rfsp.mcp.service.RecordService;
import com.rfsp.common.shiro.ShiroUtils;
import com.rfsp.mcp.controller.base.BaseController;
import com.rfsp.common.pojo.po.OrgPO;
import com.rfsp.common.pojo.po.UserPO;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RequiresRoles(value={"mcp_bank"}, logical= Logical.AND)
@Controller("BankController")
@RequestMapping("/bank")
public class BankController extends BaseController {

    @Autowired
    RecordService recordService;


    @RequestMapping ("/businessRules")
    public String businessRules(ModelMap model) {
        return "bank/businessRules";
    }
    @RequestMapping("/businessRules1")
    public String businessRules1(ModelMap model) {
        return "bank/businessRules1";
    }
    @RequestMapping("/businessRules2")
    public String businessRules2(ModelMap model) {
        return "bank/businessRules2";
    }
    @RequestMapping("/businessRules1/confirm")
    public ModelAndView confirm1(ModelMap model, HttpServletRequest request){
//        String c = request.getParameter("c");
//        if(c==null){
//            return redirectView("/login/dash?msg=请勾选我已阅读并清楚以上业务规则");
//        }
        OrgPO org = ShiroUtils.getAttribute("org");
        AgreementRecordPO apo = recordService.getRecord(org.getId(), RecordService.AGREEMENT_SUQIAN);
        if(apo != null){
            apo.setState("1");
            recordService.update(apo);
        }
        //存储
        return redirectView("/login/dash");
    }

    @RequestMapping("/businessRules2/confirm")
    public ModelAndView confirm2(ModelMap model) {

        UserPO user = ShiroUtils.getAttribute("user");
        OrgPO org = ShiroUtils.getAttribute("org");

        AgreementRecordPO apo = new AgreementRecordPO();
        apo.setAgreementRecordName("医保贷业务规则-非宿迁");
        apo.setAgreementNo(RecordService.AGREEMENT_COMMON);
        apo.setOrgId(org.getId());
        apo.setUserId(user.getId());
        apo.setState("1");

        recordService.save(apo);

        return redirectView("/login/dash");
    }


    @RequestMapping("/businessRulesDownload")
    public String businessRulesDownload(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        String dirPath ="/rfpfile/templet/MCPFinanceBusinessRulesYBD.docx";

        try {
            FileUtil.downloadfile(request, response, dirPath, "医保信息查询授权书.docx");
        } catch (Exception e) {
            log.error("error", e);
        }

        return null;
    }

}
